﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DAL.Entities;

namespace DAL.EntitiesDAL
{
    public class CoGiaHanDAL : BaseDAL, UTL.IBaseDAL 
    {
        #region Implement

        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            return ExecuteQuery("sp_SelectAll_Cogiahan");
        }


        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            var sql = "sp_Delete_Cogiahan '{0}'";
            sql = string.Format(sql, id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (CoGiaHan)obj;
            var sql = "set dateformat dmy; execute sp_Insert_Cogiahan '{0}', '{1}', '{2}', '{3}' ";
            sql = string.Format(sql, o.STT_PM, o.MACUONSACH, o.NGAY.ToString("dd/MM/yyyy"), o.LANGIAHAN);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (CoGiaHan)obj;
            var sql = "set dateformat dmy; execute sp_Update_Cogiahan '{0}', '{1}', '{2}', '{3}' ";
            sql = string.Format(sql, o.STT_PM, o.MACUONSACH, o.NGAY.ToString("dd/MM/yyyy"), o.LANGIAHAN);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            var sql = @"Select c.STT_PM, b.MACUONSACH, f.TENDAUSACH, g.TENTHELOAI, h.TENNHAXB, LTRIM(rtrim(e.HODOCGIA)) + ' ' + LTRIM(rtrim(e.TENDOCGIA)) as HOVATEN, d.NGAY, a.LANGIAHAN
                                from tb_Cogiahan a 
                                                    join tb_Cuonsach b on a.MACUONSACH=b.MACUONSACH 
                                                    join tb_Phieumuon c on a.STT_PM=c.STT_PM 
                                                    join tb_Thoidiem d on a.NGAY=d.NGAY
                                                    join tb_Thedocgia e on c.MADOCGIA=e.MADOCGIA
                                                    join tb_Dausach f on b.MADAUSACH=f.MADAUSACH
                                                    join tb_Theloai g on f.MATHELOAI=g.MATHELOAI
                                                    join tb_Nhaxuatban h on f.MANHAXB=h.MANHAXB   ";
            sql = string.Format(sql, name);
            return ExecuteQuery(sql);
        }

        public DataTable Search(DAL.Entities.CoGiaHan o)
        {
            string sql;
            if (o.STT_PM != "")
                sql = @"Select c.STT_PM, b.MACUONSACH, f.TENDAUSACH, g.TENTHELOAI, h.TENNHAXB, LTRIM(rtrim(e.HODOCGIA)) + ' ' + LTRIM(rtrim(e.TENDOCGIA)) as HOVATEN, d.NGAY, a.LANGIAHAN
                         from tb_Cogiahan a 
                                            join tb_Cuonsach b on a.MACUONSACH=b.MACUONSACH 
                                            join tb_Phieumuon c on a.STT_PM=c.STT_PM 
                                            join tb_Thoidiem d on a.NGAY=d.NGAY
                                            join tb_Thedocgia e on c.MADOCGIA=e.MADOCGIA
                                            join tb_Dausach f on b.MADAUSACH=f.MADAUSACH
                                            join tb_Theloai g on f.MATHELOAI=g.MATHELOAI
                                            join tb_Nhaxuatban h on f.MANHAXB=h.MANHAXB 
                        Where c.STT_PM like '%" + o.STT_PM + "%'  ";
            else
                sql = @"Select  c.STT_PM, b.MACUONSACH, f.TENDAUSACH, g.TENTHELOAI, h.TENNHAXB, LTRIM(rtrim(e.HODOCGIA)) + ' ' + LTRIM(rtrim(e.TENDOCGIA)) as HOVATEN, d.NGAY, a.LANGIAHAN
                                from tb_Cogiahan a 
                                            join tb_Cuonsach b on a.MACUONSACH=b.MACUONSACH 
                                            join tb_Phieumuon c on a.STT_PM=c.STT_PM 
                                            join tb_Thoidiem d on a.NGAY=d.NGAY
                                            join tb_Thedocgia e on c.MADOCGIA=e.MADOCGIA
                                            join tb_Dausach f on b.MADAUSACH=f.MADAUSACH
                                            join tb_Theloai g on f.MATHELOAI=g.MATHELOAI
                                            join tb_Nhaxuatban h on f.MANHAXB=h.MANHAXB 
                                Where b.MACUONSACH like '%" + o.MACUONSACH + "%' ";
            return ExecuteQuery(sql);
        }


        #endregion

        #region Other

        public DataTable Select_Max()
        {
            var sql = @"Select Max(STT_PM) as Ma From tb_Cogiahan";
            return ExecuteQuery(sql);
        }


        #endregion
    }
}
